package com.cc.pokergame;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.cc.pokergame.databinding.ActivityHomeBinding;
import com.cc.pokergame.model.Player;
import com.cc.pokergame.model.TableModel;

import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends AppCompatActivity {
    private ActivityHomeBinding homeBinding;
    private Player player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        homeBinding = ActivityHomeBinding.inflate(getLayoutInflater());
        setContentView(homeBinding.getRoot());

        // 加载用户
        loadUserInfo();

        // 显示用户信息
        bindingData();

        // 配置
        initUI();
    }

    private void initUI(){
        // 创建一个布局管理器对象
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false);
        homeBinding.recyclerview.setLayoutManager(layoutManager);

        // 创建自定义适配器对象
        GameAdapter gameAdapter = new GameAdapter();
        // 通过适配器设置内容
        homeBinding.recyclerview.setAdapter(gameAdapter);

        gameAdapter.setData(getTableModels());
    }

    private List<TableModel> getTableModels(){
        ArrayList<TableModel> models = new ArrayList<>();
        models.add(new TableModel("新手场", 1000, 234, 1000, 20000));
        models.add(new TableModel("初级场", 2000, 212, 2000, 30000));
        models.add(new TableModel("进阶场", 3000, 235, 3000, 40000));
        models.add(new TableModel("高级场", 4000, 651, 5000, 60000));
        models.add(new TableModel("大师场", 5000, 831, 6000, 80000));
        models.add(new TableModel("地狱场", 6000, 900, 9000, 99999));
        return models;
    }

    /**
     * 加载用户信息
     */
    private void loadUserInfo() {
        SharedPreferences userInfo = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        String name = userInfo.getString("name", "");
        int money = userInfo.getInt("money", 0);
        String headName = userInfo.getString("headName", "");

        // 拼接头像图片文件的完整路径，得到当前文件目录使用.getAbsolutePath()得到文件目录路径字符串
        String headFilePath = getFilesDir().getAbsolutePath()+ "/" + headName;
        player = new Player(headFilePath, name, money);
    }

    private void bindingData() {
        Glide.with(this)
                .load(player.avatarUri())
                .apply(RequestOptions.circleCropTransform()) // 将图片剪切为圆形
                .into(homeBinding.ivHead);
//        homeBinding.ivHead.setImageURI(player.headUri());
        homeBinding.tvName.setText(player.getName());
        homeBinding.tvMoney.setText(player.getMoney() + "");
    }
}